﻿function Cookie(){
    this.cookies = [];
    /**
    * @param name
    **/
    this.get = function(name) {
       this._reset();
       return this.cookies[name];
    }
    /**
    * name & value must be Required 
    * 
    * @param {string} name (Required)
    * @param {string} value (Required)
    * @param {integer} expires (optional, [Units:seconde] , exp (one day): 1*24*60*60)
    * @param {string} path (optional, exp: '/')
    * @param {string} domain (optional, exp: '.a.com')
    * @param {boolean} secure (optional, exp: false)
    **/
    this.set = function() {
       var args = this.set.arguments;
       var _num = args.length;
       if (_num < 2) return;
      
       var _cookie = args[0] + '=' + this._encode(args[1]);
      
       if (_num >= 3) {
        var now = new Date();
        var _expires = new Date(now.getTime() + args[2]);
        _cookie += ';expires=' + _expires.toUTCString();
       }
      
       if (_num >= 4) {
        _cookie += ';path=' + args[3]; 
       }
      
       if (_num >= 5) {
        _cookie += ';domain=' + args[4];
       }
      
       if (_num >= 6) {
        _cookie += ';secure';
       }
      
       document.cookie = _cookie;
    }
    this.print_r = function() {
       this._reset();
       for (var i in this.cookies) {
        document.write(i + ' -> ' + this.cookies[i] + '<br />\n');
       }
    }
    this._reset = function() {
       var cookie = document.cookie.split(';');
      
       var _num = cookie.length;
       for (var i = 0; i < _num; i++) {
        var _arr = cookie[i].split('=');
        var _name = this._trim(_arr[0]);
        var _value = '';
        if (typeof _arr[1] != 'undefined') {
         _value = this._decode(this._trim(_arr[1]));
        }
        this.cookies[_name] = _value;
       }
    }

    this._trim = function(_str) {
       return _str.replace(/(^\s+)|(\s*$)/g,''); 
    }
    this._encode = function(_str) {
       return encodeURI(_str);
    }
    this._decode = function(_str) {
       return decodeURI(_str);
    }
}